@extends('admin.layout.layout', ['row' => @$page])

@section('contain')
    <div class="portlet">
        <div class="portlet-title">
            <div class="caption">
                <x-portlet-breadcrumb :page="$page"></x-portlet-breadcrumb>
            </div>
            <div class="actions"></div>
        </div>

        <form action="/{{ request()->path() }}/../" method="post" onsubmit="return checkVal(this)" class="return-form">
            @csrf
            <input type="text" name="_previous_" value="{{ request()->header('referer') }}" hidden>
            <div class="form-item form-s1">
                <div class="form-label" required="">{{ ts('ROLLnumber') }}</div>
                <div class="form-input">
                    <select name="roll_number" id="" class="form-control sq-rolls"></select>
                </div>
            </div>

            <div class="form-item form-s1 film_type_box d-none">
                <div class="form-label">{{ ts('mxh') }}</div>
                <div class="form-input">
                    <select name="film_type_id" class="form-control" disabled></select>
                </div>
            </div>

            <div class="form-item form-s1 user-box d-none">
                <div class="form-label">{{ ts('YGMSMC') }}</div>
                <div class="form-input">
                    <input type="text" name="user_id" hidden>
                    <input type="text" name="user_name" class="form-control" readonly>
                </div>
            </div>

            <div class="form-item form-s1 length-box d-none">
                <div class="form-label" required="">{{ ts('sycd') }}</div>
                <div class="form-input">
                    <div class="input-group">
                        <input name="length" class="form-control" readonly>

                        <div class="input-group-prepend">
                            <span class="input-group-text">cm</span>
                        </div>
                    </div>
                </div>
            </div>

            <div class="form-item form-s1">
                <div class="form-label" required="">{{ ts('EXPLAIN') }}</div>
            </div>
            <div class="form-item form-s1 px-4">
                <textarea name="note" id="" cols="30" rows="10" class="form-control" required>{{ $data->note ?: old('note') }}</textarea>
            </div>
            <div class="mt-4">
                <button class="btn btn-info">{{ ts('SUBMIT') }}</button>
            </div>
        </form>

    </div>


@endsection

@section('script')
    <script>
        function getResidueLengthByRollNumberAndGmsId(roll_number, gmsid) {
            roll_number = $('[name="roll_number"]').val()
            gmsid = $('select[name="ygmsid"]').val()
            if (roll_number == '' || gmsid=='') {
                return false
            }
            request.get('/admin/ajax/getResidueLengthByRollNumberAndGmsId', {
                params: {
                    roll_number: roll_number,
                    gmsid: gmsid,
                }
            }).then( function(d) {
                var length = d.data
                if (length < 0) {
                    length = 0
                }
                $('.form-item').find('[name="length"]').val(length)
                // $('.form-item').find('.residue-length b').text(length)
            })
        }

        function getGMSByRollNumber(roll_number) {
            var roll_number = String(roll_number).trim()
            if (roll_number === '' || roll_number==='null' || roll_number===null) {
                $('select[name="ygmsid"]').empty()
                return false
            }
            request.get('/admin/ajax/getRGMS', {
                params: {
                    roll_number: roll_number
                }
            }).then( function(d) {
                var data = d.data
                $('select[name="ygmsid"]').val(' ')
                $('select[name="ygmsid"]').empty().append(makeSelectOptionsOnly(data));
                $('[name="ygmsid"]').trigger('change')
                getResidueLengthByRollNumberAndGmsId()
            })
        }

        function getRestlen() {
            var roll_number = $('.sq-rolls[name="roll_number"]').val()
            if (roll_number === '' || roll_number==='null' || roll_number===null) {
                return false
            }
            request.get('/admin/ajax/getARRN2', {
                params: {
                    roll_number: roll_number
                }
            })
                .then(function(d) {
                    var data = d.data;
                    $('.form-item.user-box').removeClass('d-none').find('[name="user_name"]').val(data.user_name);
                    $('.form-item.user-box [name="user_id"]').val(data.user_id);
                    $('.form-item.length-box').removeClass('d-none').find('[name="length"]').val(data.restlen);
                    
                    /*if (data && data.checked == true && data.restlen == data.length) {
                        $('.form-item').find('[name="length"]').val(data.restlen)
                    } else {
                        toastr.error('卷号剩余长度不足，不允许退货！');
                        $('body .sq-rolls').select2('val', ' ');
                        $('.film_type_box [name="film_type_id"]').val(' ');
                        $('[name="length"]').val('0');
                    }*/

                })
                .catch(function (d) {
                    console.log('catch', d)
                })
        }

        $(document).ready(function () {

            if ($('.film_type_box').length >= 1) {
                $('.film_type_box').find('[name="film_type_id"]').empty().append(makeSelectOptionsOnly(coverages_data.filmtype, 0));
            }

            $('select[name="roll_number"]').on('change', function () {
                getRestlen($(this).val())
                var val = $(this).val();
                var roll = rolls_front_data[val];
                console.log(roll)
                if (roll && roll.film_type_id) {
                    $('.film_type_box').removeClass('d-none').find('[name="film_type_id"]').val(roll.film_type_id)
                }
                console.log('roll_number change')
            });
            /*不做删减*/
            $('input[name="length"]').on('change input propertychange', function () {
                var val = $(this).val()
                var max = $('.form-item').find('.residue-length b').text()
                val = Number(val)
                max = Number(max)
                if (val > max) {
                    val = max
                }
                if (val < 0) {
                    val = 0
                }
                $(this).val(val)
            });

            $('select.sq-rolls').select2({
                ajax: {
                    type:'get',
                    // url: '/admin/ajax/getURRL',
                    url: '/admin/ajax/getARRN',
                    dataType: 'json',
                    delay: 250,
                    data: function (params) {
                        return {
                            roll_number: params.term,
                        };
                    },
                    processResults: function (data) {
                        data = data['data']
                        items = []
                        for (var i = 0; i <= data.length - 1; i++) {
                            items[i] = {
                                id: data[i].roll_number,
                                text: data[i].roll_number
                            }
                            window.rolls_front_data[data[i].roll_number] = data[i]
                        }
                        return {
                            results: items
                        };
                    },
                },
                selectOnClose: true
            });
        });

        function getSdmc(id) {
            return new Promise(function (resolve, reject) {
                if (!id) {
                    return reject(null)
                }
                request.get('/admin/ajax/getSdmc', {
                    params: {
                        user_id: id
                    }
                })
                    .then(function (d) {
                        resolve(d)
                    })
                    .catch(function (d) {
                        reject(d)
                    })
            })
        }
    </script>

@endsection